import Storage from './driver-localstorage';

const Token = {
  driver: Storage,
  token_key: 'X-USER-TOKEN',

  setOptions(options) {
    /**
     * set driver
     */
    if (options.hasOwnProperty('driver')) {
      if(options.driver === 'localstorage') {
        this.driver = Storage
      }
    }

    /**
     * set type
     */
    if(options.hasOwnProperty('type')) {
      if(options.type === 'user') {
        this.token_key = 'X-USER-TOKEN'
      }

      if(options.type === 'admin') {
        this.token_key = 'X-ADMIN-TOKEN'
      }
    }
  },

  init(options) {
    this.setOptions(options);
  },

  isValid: function () {
    return !!this.getToken();
  },

  getToken: function () {
    return this.driver.get(this.token_key);
  },

  forgetToken: function () {
    return this.driver.remove(this.token_key)
  },
  setToken(token) {
    this.driver.set(this.token_key, token);
  }
};

export default Token;
